#include "stdafx.h"
#include "modules.h"
#include "command.h"

void clone(int n)
{
	string url = MixFeeds2URL(project().at(n).feeds, project().at(n).url);
	string dir = project().at(n).dir;
	string mkdir = "if not exist " + dir + " " MKDIR " " + dir;
	if (KERNEL_SPACE().re_clone)
	{
		string re_clone = "if exist " + dir + " rmdir /q /s " + dir;
		cmd(re_clone);
	}
	if (KERNEL_SPACE().mkdir)
	{
		cmd(mkdir.data());
		string git_clone ="if not exist " + dir + "\.git " GIT " clone " + url + " " + dir;
		cout << "Exec:" << git_clone << endl;
		if (KERNEL_SPACE().clone)
			cmd(git_clone.data());
	}
}

void pull(int n)
{
	string dir = project().at(n).dir;
	string git_pull = "set WORKDIR=%CD% && cd /d " + dir + "&&" GIT " pull " + MixFeeds2URL(project().at(n).feeds, project().at(n).url) + "&&cd /d %WORKDIR%";
	cmd(git_pull.data());
}

int clone()
{
	DEBUG_LINE cout << "Download Thread is Begin!" << endl;
	DEBUG_LINE cout << "Get Source Number:" << project().size() << endl;
#pragma omp parallel for
		for (int n = 0; n < project().size(); n++)
//		if (!KERNEL_SPACE().spec_operat || !strcmp(project().at(n).name.data(),KERNEL_SPACE().operaton_project.data()))
			clone(n);
	return 0;
}

class mod m_clone("clone", clone, "clone\tClone a Source form feeds");

int mkdir()
{
	KERNEL_SPACE().clone = false;
	return clone();
}

class mod m_mkdir("mkdir", mkdir, "mkdir\tOnly Create Dir");

int pull()
{

	if (!KERNEL_SPACE().operaton_project.empty())
	{
#pragma omp parallel for
		for (int n = 0; n < project().size(); n++)
			pull(n);
	}
	else{
		for (unsigned int n = 0; n < project().size(); n++)
		if (!strcmp(KERNEL_SPACE().operaton_project.data(), project().at(n).name))
			pull(n);
	}
	return 0;
}

class mod m_pull("pull", pull, "pull\tupdate a Source form feeds");